#!/bin/sh

# Start cfengine:
cfengine_start() {
  if [ -x /usr/sbin/cfexecd ]; then
    # Make sure localhost keys exist first
    if [ ! -f /var/cfengine/ppkeys/localhost.priv ]; then
      /usr/sbin/cfkey
    fi
    echo "Starting Cfengine:  /usr/sbin/cfexecd"
    /usr/sbin/cfexecd
  fi
}

# Stop cfengine:
cfengine_stop() {
  /bin/killall cfenvd 2> /dev/null
  /bin/killall cfexecd 2> /dev/null
  /bin/killall cfservd 2> /dev/null
}

# Restart cfengine:
cfengine_restart() {
  cfengine_stop
  sleep 1
  cfengine_start
}

# Reload cfengine:
cfengine_reload() {
  /bin/killall -HUP cfenvd
  /bin/killall -HUP cfexecd
  /bin/killall -HUP cfservd
}

case "$1" in
'start')
  cfengine_start
  ;;
'stop')
  cfengine_stop
  ;;
'restart')
  cfengine_restart
  ;;
'reload')
  cfengine_reload
  ;;
*)
  echo "usage $0 start|stop|restart|reload"
esac
